package com.flying.acls.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public static PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }

//    @Bean
//    @Override
//    protected UserDetailsService userDetailsService() {
//        return new UserService();
//    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/home").permitAll()
                .antMatchers("/hello").hasAuthority("ROLE_ADMIN")
                .antMatchers(HttpMethod.GET, "/testgetpost").hasAuthority("ROLE_USER")
                .antMatchers(HttpMethod.POST, "/testgetpost").hasAuthority("ROLE_ADMIN")
                .anyRequest().authenticated()
                .and().formLogin().loginPage("/login").permitAll()
                .and().logout().logoutSuccessUrl("/home").permitAll().invalidateHttpSession(true)
                .and().rememberMe().tokenValiditySeconds(30 * 60);

        // support h2-console.
        http.csrf().disable();
        http.headers().frameOptions().disable();
    }

}
